from random import randint
import pygame
import math


class Fa_bullet(pygame.sprite.Sprite):
    def __init__(self, doc_image1, doc_image2, speed, power):
        super(Fa_bullet, self).__init__()
        self.image1 = pygame.image.load(doc_image1).convert_alpha()
        self.image2 = pygame.image.load(doc_image2).convert_alpha()
        self.rect = self.image1.get_rect()
        self.speed = speed
        self.active = False
        self.mask = pygame.mask.from_surface(self.image1)
        self.power = power
        self.show_image = self.image1

    def Move(self , bg_size):
        if self.rect.bottom < 0 or self.rect.top > bg_size[1]:
            self.active = False
        else:
            self.rect.top -= self.speed[0]
        if self.rect.left < 0 and self.rect.right > bg_size[0]:
            self.active = False
        else:
            self.rect.left-=self.speed[1]

    def reset(self,position):
        self.rect.left = position[0]
        self.rect.top = position[1]
        self.active = True
    def turn_image(self):
        if self.show_image == self.image1:
            self.show_image = self.image2
        else:
            self.show_image = self.image1

class Bullet1(Fa_bullet):
    def __init__(self):
        Fa_bullet.__init__(self,"images/bullet1.png", "images/bullet2.png", (12,0),2)



class Bullet2(Fa_bullet):
    def __init__(self):
        Fa_bullet.__init__(self,"images/bullet2_1.png" , "images/bullet2_2.png",(0,0),0.4)

    def Move(self , position):
        self.rect.left = position[0] - (self.rect.right - self.rect.left) / 2 + 3
        self.rect.bottom = position[1] + 30
        self.active = True

    def reset(self,postion):
        self.rect.left = postion[0] - (self.rect.right - self.rect.left)/2 + 3
        self.rect.bottom= postion[1] + 30
        self.active = True

class Bullet3(Fa_bullet):
    def __init__(self):
        Fa_bullet.__init__(self,"images/bullet3_1.png" ,"images/bullet3_2.png", (6,0),0.5)

    def reset(self,position):
        self.rect.left = position[0] - 70
        self.rect.top = position[1] +20
        self.active = True

class Boss_bullet(Fa_bullet):
    def __init__(self,speed):
        Fa_bullet.__init__(self, "images/boss_bullet1.png", "images/boss_bullet2.png", speed, 2)
        self.speed

    def reset(self, position,speed):
        self.rect.left = position[0]+100
        self.rect.top = position[1]+300
        self.active = True
        self.speed = speed

class Skill_Bullet(Fa_bullet):
    def __init__(self,speed):
        Fa_bullet.__init__(self, "images/me2_s.png", "images/me2_s.png", speed, 4)
        self.target = 0
        # self.dict = randint(0,180)
        self.dict = 0
        self.has_target = True
        self.speed1 = 1

    def reset(self , position , speed):
        Fa_bullet.reset(self,position)
        self.speed1 = speed


    def Move(self , enemy):
        if self.has_target:
            dx =   enemy.rect.centerx -  self.rect.centerx
            dy =   self.rect.centery - (enemy.rect.top + 20)

            #获得角度
            dict = math.acos( dx / math.sqrt(dx*dx + dy * dy))

            #开始旋转 3度/10帧 ，判断往哪转，或者不转
            if self.dict < dict - math.pi / 180 * 3:
                self.dict += math.pi / 180 * 3
            elif self.dict > dict +math.pi / 180 * 3:
                self.dict -= math.pi / 180 * 3
            else:
                self.dict = dict
        self.rect.top -= self.speed1 * math.sin(self.dict)
        self.rect.left += self.speed1 * math.cos(self.dict)

    # def reset(self,position):
    #     Fa_bullet.reset(self,position)
    #     self.has_target = True
        # if dx ==     0 :
        #     self.dict = 90
        # else:
        #     self.dict = math.atan(dy/dx) * 180 / 3.1415



class Boss_ball(Fa_bullet):
    def __init__(self):
        Fa_bullet.__init__(self,"images/boss_skill2_2.png" ,"images/boss_skill2_2.png", [-7,0],0.2)
        self.angle = 0

